#include "base.pb.h"
#include "user.pb.h"
#include "etcd.hpp"
#include "logger.hpp"
#include "channel.hpp"
#include "utils.hpp"
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>

DEFINE_bool(mode, false, "运行模式： true-发布模式； false-测试模式");
DEFINE_string(file, "", "发布模式日志更新地址");
DEFINE_int32(level, 1, "默认日志等级");

DEFINE_string(etcd_host, "127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(instance_service, "/service/user_service", "服务监控根目录");
DEFINE_string(user_nickname, "zhiang", "");
zhiang::ServiceChannel::ChannelPtr channel;

// TEST(注册, 密码) {
//     zhiang::UserService_Stub stub(channel.get());
//     brpc::Controller cntl;
//     zhiang::UserRegisterReq req;
//     zhiang::UserRegisterRsp resp;
//     req.set_nickname(FLAGS_user_nickname);
//     req.set_request_id(zhiang::generate_uid());
//     req.set_password("123456");
//     // 4. 发起EchoRpc调用
//     stub.UserRegister(&cntl, &req, &resp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(resp.success());
// }

// TEST(登录, 密码) {
//     zhiang::UserService_Stub stub(channel.get());
//     brpc::Controller cntl;
//     zhiang::UserLoginReq req;
//     zhiang::UserLoginRsp resp;
//     req.set_nickname(FLAGS_user_nickname);
//     req.set_request_id(zhiang::generate_uid());
//     req.set_password("123456");
//     // 4. 发起EchoRpc调用
//     stub.UserLogin(&cntl, &req, &resp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(resp.success());
// }

TEST(设置头像, 1) {
    zhiang::UserService_Stub stub(channel.get());
    brpc::Controller cntl;
    zhiang::SetUserAvatarReq req;
    zhiang::SetUserAvatarRsp resp;
    req.set_request_id(zhiang::generate_uid());
    req.set_avatar("love zhiang~~~~");
    req.set_user_id("d82cb0f2-a5alH0-A9I5GT-0003");
    req.set_session_id(zhiang::generate_uid());
    // 4. 发起EchoRpc调用
    stub.SetUserAvatar(&cntl, &req, &resp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(resp.success());
}

// TEST(设置昵称, 1) {
//     zhiang::UserService_Stub stub(channel.get());
//     brpc::Controller cntl;
//     zhiang::SetUserNicknameReq req;
//     zhiang::SetUserNicknameRsp resp;
//     req.set_request_id(zhiang::generate_uid());
//     req.set_nickname("ZHIANG");
//     req.set_user_id("d82cb0f2-HlT9Vg-3KyAOy-0002");
//     req.set_session_id(zhiang::generate_uid());
//     // 4. 发起EchoRpc调用
//     stub.SetUserNickname(&cntl, &req, &resp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(resp.success());
// }

// TEST(设置签名, 1) {
//     zhiang::UserService_Stub stub(channel.get());
//     brpc::Controller cntl;
//     zhiang::SetUserDescriptionReq req;
//     zhiang::SetUserDescriptionRsp resp;
//     req.set_request_id(zhiang::generate_uid());
//     req.set_description("love atri");
//     req.set_user_id("d82cb0f2-HlT9Vg-3KyAOy-0002");
//     req.set_session_id(zhiang::generate_uid());
//     // 4. 发起EchoRpc调用
//     stub.SetUserDescription(&cntl, &req, &resp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(resp.success());
// }

// TEST(用户个人信息获取, t1) {
//     zhiang::UserService_Stub stub(channel.get());
//     brpc::Controller cntl;
//     zhiang::GetUserInfoReq req;
//     zhiang::GetUserInfoRsp resp;
//     req.set_request_id(zhiang::generate_uid());
//     req.set_user_id("d82cb0f2-HlT9Vg-3KyAOy-0002");
//     // 4. 发起EchoRpc调用
//     stub.GetUserInfo(&cntl, &req, &resp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(resp.success());
//     ASSERT_EQ("ZHIANG", resp.user_info().nickname());
//     ASSERT_EQ("d82cb0f2-HlT9Vg-3KyAOy-0002", resp.user_info().user_id());
//     ASSERT_EQ("hahaha", resp.user_info().avatar());
//     ASSERT_EQ("love atri", resp.user_info().description());
// }

// TEST(用户个人信息获取, 多人) {
//     zhiang::UserService_Stub stub(channel.get());
//     brpc::Controller cntl;
//     zhiang::GetMultiUserInfoReq req;
//     zhiang::GetMultiUserInfoRsp resp;
//     req.set_request_id(zhiang::generate_uid());
//     req.add_users_id("d82cb0f2-HlT9Vg-3KyAOy-0002");
//     req.add_users_id("d82cb0f2-ApPS4K-eLaZ7k-0004");
//     req.add_users_id("d82cb0f2-a5alH0-A9I5GT-0003");
//     // 4. 发起EchoRpc调用
//     stub.GetMultiUserInfo(&cntl, &req, &resp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(resp.success());
//     auto user_map = resp.mutable_users_info();
//     zhiang::UserInfo zhiang = (*user_map)["d82cb0f2-HlT9Vg-3KyAOy-0002"];
//     zhiang::UserInfo atri = (*user_map)["d82cb0f2-a5alH0-A9I5GT-0003"];
//     zhiang::UserInfo 亚托莉 = (*user_map)["d82cb0f2-ApPS4K-eLaZ7k-0004"];
//     ASSERT_EQ("ZHIANG", zhiang.nickname());
//     ASSERT_EQ("亚托莉", 亚托莉.nickname());
//     ASSERT_EQ("atri", atri.nickname());
//     ASSERT_EQ("love atri", zhiang.description());
//     ASSERT_EQ("", 亚托莉.description());
//     ASSERT_EQ("", atri.description());
//     ASSERT_EQ("hahaha", zhiang.avatar());
//     ASSERT_EQ("", atri.avatar());
//     ASSERT_EQ("", 亚托莉.avatar());
//     ASSERT_EQ("", zhiang.phone());
//     ASSERT_EQ("", 亚托莉.phone());
//     ASSERT_EQ("", atri.phone());
// }

std::string cid;

void get_code(const std::string& phone) {
    zhiang::UserService_Stub stub(channel.get());
    brpc::Controller cntl;
    zhiang::PhoneVerifyCodeReq req;
    zhiang::PhoneVerifyCodeRsp resp;
    req.set_request_id(zhiang::generate_uid());
    req.set_phone_number(phone);
    stub.GetPhoneVerifyCode(&cntl, &req, &resp, nullptr);
    if (cntl.Failed() == true || resp.success() == false) {
        LOG_DEBUG("get_code: cntl.Failed-{}  resp.success-{} ", cntl.Failed(), resp.success());
        return;
    }
    cid = resp.verify_code_id();
}

// TEST(手机号注册, 1) {
//     get_code("17377376823");
//     zhiang::UserService_Stub stub(channel.get());
//     brpc::Controller cntl;
//     zhiang::PhoneRegisterReq req;
//     zhiang::PhoneRegisterRsp resp;
//     req.set_request_id(zhiang::generate_uid());
//     req.set_phone_number("17377376823");
//     std::cout << "请输入验证码: ";
//     std::string code; cin >> code;
//     req.set_verify_code(code);
//     req.set_verify_code_id(cid);
//     // 4. 发起EchoRpc调用
//     stub.PhoneRegister(&cntl, &req, &resp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(resp.success());
// }

// TEST(手机号登录, 1) {
//     get_code("17377376823");
//     zhiang::UserService_Stub stub(channel.get());
//     brpc::Controller cntl;
//     zhiang::PhoneLoginReq req;
//     zhiang::PhoneLoginRsp resp;
//     req.set_request_id(zhiang::generate_uid());
//     req.set_phone_number("17377376823");
//     std::cout << "请输入验证码: ";
//     std::string code; cin >> code;
//     req.set_verify_code(code);
//     req.set_verify_code_id(cid);
//     // 4. 发起EchoRpc调用
//     stub.PhoneLogin(&cntl, &req, &resp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(resp.success());
//     LOG_DEBUG("login_session_id: {}", resp.login_session_id());
// }


// TEST(手机号设置, 1) {
//     get_code("17377376823");
//     zhiang::UserService_Stub stub(channel.get());
//     brpc::Controller cntl;
//     zhiang::SetUserPhoneNumberReq req;
//     zhiang::SetUserPhoneNumberRsp resp;
//     req.set_request_id(zhiang::generate_uid());
//     std::cout << "请输入你的id: ";
//     std::string uid; std::cin >> uid;
//     req.set_user_id(uid);
//     req.set_session_id(zhiang::generate_uid());
//     req.set_phone_number("19317245883");
//     std::cout << "请输入验证码: ";
//     std::string code; cin >> code;
//     req.set_phone_verify_code(code);
//     req.set_phone_verify_code_id(cid);
//     // 4. 发起EchoRpc调用
//     stub.SetUserPhoneNumber(&cntl, &req, &resp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(resp.success());
// }

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    init_logger(FLAGS_mode, FLAGS_file, FLAGS_level);
    // 1. 构造R赔偿信道管理对象
    zhiang::ServiceManage::ptr sm = std::make_shared<zhiang::ServiceManage>();
    sm->declared(FLAGS_instance_service);
    auto _put_cb = std::bind(&zhiang::ServiceManage::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    auto _del_cb = std::bind(&zhiang::ServiceManage::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    // 2. 构造服务发现对象
    zhiang::Discovery::ptr dClient = std::make_shared<zhiang::Discovery>(FLAGS_etcd_host, FLAGS_base_service, _put_cb, _del_cb);
    channel = sm->choose(FLAGS_instance_service);
    if (!channel)
    {
        LOG_ERROR("user模块channel为空");
        return -1;
    }
    return RUN_ALL_TESTS();
}